package com.cuit.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {
    @Bean("taskExecutor")
    public Executor asyncServiceExecutor(){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();

        threadPoolTaskExecutor.setCorePoolSize(10); // 设置核心线程数

        threadPoolTaskExecutor.setMaxPoolSize(40); // 设置最大线程数

        threadPoolTaskExecutor.setQueueCapacity(Integer.MAX_VALUE); // 设置队列大小

        threadPoolTaskExecutor.setThreadNamePrefix("cfy"); // 设置线程默认名字

        threadPoolTaskExecutor.setKeepAliveSeconds(100); // 设置线程活跃时间

        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true); // 等待所有任务结束后再关闭线程

        threadPoolTaskExecutor.initialize(); // 初始化

        return threadPoolTaskExecutor;
    }
}
